package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BusinessAbodeMapper;
import com.koron.css2.baseConfig.mapper.WaterTypeMapper;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.meterService.MeterService;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.PrintInterfaceImpl;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 客户管理  打印新卡
 * @author 10950
 *
 */
public class XkPrint implements ServerInterface{

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			//入参转换
			UserBean queryBean = JsonUtils.objectToPojo(req.getData(), UserBean.class);
			MessageBean<String> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", String.class);
			
			//判断是否打印
			if (queryBean != null ) {
				Map<String, String> m =  new HashMap<String, String>();
				//获取用户信息
				UserInfoMapper mapper = factory.getMapper(UserInfoMapper.class);
				UserBean user = mapper.getUserInfoByUserNo(queryBean.getUserNo(), UserBean.USER);// 获取用户信息
				
				ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String,String>>();
				CtmInfoMapper ctmMapper = factory.getMapper(CtmInfoMapper.class);

				CtmInfoBean ctmInfo = ctmMapper.getCtmInfoByCtmNo(user.getCtmNo(), CtmInfoBean.CTM);// 获取客服信息
				m.put("userNo", queryBean.getUserNo());
				m.put("ctmName", ctmInfo.getCtmName());
				m.put("ctmAddr", ctmInfo.getCtmAddr());
				// 获取水表信息
				MsInfoDto msInfo=null;
				if (!StringUtils.isEmpty(user.getMeterNo())) {
					MeterService meterSrvice = new MeterService();
					msInfo = ADOConnection.runTask("_default", meterSrvice, "getMsInfoByNo", MsInfoDto.class,
							user.getMeterNo(),userInfo.getCurWaterCode());
					m.put("meterBoreName", msInfo.getMeterBoreName());
				}
				
				// 获取用水类型
				if (!StringUtils.isBlank(user.getUseWaterType())) {
					WaterTypeMapper waterTypeMapper = factory.getMapper(WaterTypeMapper.class);
					m.put("useWaterTypeName", waterTypeMapper.findWaterTypeById(user.getUseWaterType()).getWaterTypeName());
				}
				// 获取营业所名称
				if (!StringUtils.isEmpty(user.getBusinessAbode())) {
					BusinessAbodeMapper busniessAbodemapper = factory.getMapper(BusinessAbodeMapper.class);
					m.put("businessAbodeName", busniessAbodemapper.findBusinessAbodeById(user.getBusinessAbode()).getAbodeName());
				}
				data.add((HashMap<String, String>) m);
				//设置打印数据
				String key = PrintInterfaceImpl.setPrintData(data);
				//返回key
				msg.setData(key);
				
			} 
			return msg;
		} catch (Exception e) {
			logger.error("打印新卡失败", e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "打印失败", null);
		}
	}

}
